package com.atguigu.controller;


import com.atguigu.base.BaseController;
import com.atguigu.entity.Role;
import com.atguigu.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    private static final String PAGE_INDEX = "role/index";
    private static final String PAGE_CREATE = "role/create";
    private static final String ACTION_LIST = "redirect:/role";
    private static final String PAGE_EDIT = "role/edit";

    @Autowired //依赖注入
    RoleService roleService;

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id){
        roleService.delete(id);
        return ACTION_LIST;
    }

    @RequestMapping("/update")
    public String update(Role role, Map map, HttpServletRequest request){
        roleService.update(role);
        return this.successPage ("添加成功", request);
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, Map map){
        Role role = roleService.getById(id);
        map.put ("role",role);
        return PAGE_EDIT;
    }

    @RequestMapping("/save")
    public String save(Role role, Map map, HttpServletRequest request){ //springMVC框架根据反射创建bean对象， 并调用参数名称的set方法，将参数封装到bean对象中
        roleService.insert(role);
        return this.successPage ("添加成功", request);

    }

    @RequestMapping("/create")
    public String carete(){
        return PAGE_CREATE;
    }

    /**
     *分页查询
     *  根据查询条件进行查询
     *         roleName = ''
     *         pageNum = 1 隐藏域
     *         pageSize = 10  隐藏域
     *
     */
    @RequestMapping
    public String index(HttpServletRequest request, Map map) {
        Map<String,Object> filters = getFilters (request);
        // 分页对象，将集合数据，  pageNum pageSize total等
        PageInfo<Role> pageInfo = roleService.findPage(filters);
        // 拿到分页对象以后传给页面
        map.put ("page",pageInfo);
        // 请求参数传给页面做回显
        map.put ("filters",filters);
        return PAGE_INDEX;
    }
  /*  @RequestMapping
    public String index(Map map ) {
        List<Role> list = roleService.findAll();
        map.put ("list", list);
        return PAGE_INDEX;
    }*/


}
